using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
using System.Drawing;

namespace TgcViewer.Utils
{
    /// <summary>
    /// Herramienta para visualizar los ejes cartesianos
    /// </summary>
    public class TgcAxisLines
    {

        /// <summary>
        /// Vertices para dibujar los ejes cartesianos
        /// </summary>
        CustomVertex.PositionColored[] lineVertices =
        {
            new CustomVertex.PositionColored( 0.0f, 0.0f, 0.0f,  Color.Red.ToArgb() ),   // red = +x Axis
            new CustomVertex.PositionColored( 1.0f, 0.0f, 0.0f,  Color.Red.ToArgb() ),
            new CustomVertex.PositionColored( 0.0f, 0.0f, 0.0f,  Color.Green.ToArgb() ), // green = +y Axis
            new CustomVertex.PositionColored( 0.0f, 1.0f, 0.0f,  Color.Green.ToArgb() ),
            new CustomVertex.PositionColored( 0.0f, 0.0f, 1.0f,  Color.Blue.ToArgb() ),  // blue = +z Axis
            new CustomVertex.PositionColored( 0.0f, 0.0f, 0.0f,  Color.Blue.ToArgb() )
        };

        bool enable;
        /// <summary>
        /// Habilita/Deshabilita el dibujado de los ejes cartesianos
        /// </summary>
        public bool Enable
        {
            get { return enable; }
            set { enable = value; }
        }

        VertexBuffer vertexBuffer;

        
        public TgcAxisLines(Device d3dDevice)
        {
            
            vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), lineVertices.Length,
                d3dDevice, Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);

            vertexBuffer.Created += new EventHandler(this.onVertexBufferCreate);
            onVertexBufferCreate(vertexBuffer, null);
        }

        private void onVertexBufferCreate(object sender, EventArgs e)
        {
            VertexBuffer buffer = (VertexBuffer)sender;
            buffer.SetData(lineVertices, 0, LockFlags.None);
        }

        internal void render(Device d3dDevice)
        {
            Vector3 pos = GuiController.Instance.FpsCamera.Position;
            Vector3 lookAt = GuiController.Instance.FpsCamera.LookAt;


            //d3dDevice.Transform.World = Matrix.Identity * Matrix.Translation(-6f, -3.5f, 10f) * Matrix.Translation(lookAt);
            d3dDevice.Transform.World = Matrix.Identity * Matrix.Translation(lookAt);


            d3dDevice.VertexFormat = CustomVertex.PositionColored.Format;
            d3dDevice.SetStreamSource(0, vertexBuffer, 0);
            d3dDevice.DrawPrimitives(PrimitiveType.LineList, 0, 3);

            d3dDevice.Transform.World = Matrix.Identity;
            
        }

    }
}
